package com.jiyinit.web.core.business.service;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.web.core.business.dao.*;
import com.jiyinit.web.core.business.dto.OrgPageRequest;
import com.jiyinit.web.core.business.entity.*;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.common.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 部门管理service
 *
 * @author zhaolaiwei
 * @version 1.0
 * @date 2017/4/18 9:33
 * @since 1.0
 */
@Service("core.orgServiceImpl")
public class OrgServiceImpl {

    @Resource(name = "core.tabCoreOrgDao")
    private TabCoreOrgDao tabCoreOrgDao;

    @Resource(name = "core.tabFunRoleDao")
    private TabFunRoleDao tabFunRoleDao;

    @Resource(name = "core.tabFunRoleRelationDao")
    private TabFunRoleRelationDao tabFunRoleRelationDao;

    @Resource(name = "core.tabCoreFunDao")
    private TabCoreFunDao  tabCoreFunDao;

    @Resource(name ="core.tabOrgFunDao")
    private TabOrgFunDao tabOrgFunDao;

    @Resource(name = "core.tabCoreUserDao")
    private TabCoreUserDao tabCoreUserDao;

    /**
     * 根据部门ID查询部门信息
     *
     * @param orgId
     * @return
     */
    public TabCoreOrgEntity selectById(Integer orgId) {
        return tabCoreOrgDao.selectById(orgId);
    }

    /**
     * 根据父部门ID查询部门信息
     *
     * @param parentId
     * @return
     */
    public List<TabCoreOrgEntity> selectByParentId(Integer parentId) {
        return tabCoreOrgDao.selectByParentId(parentId);
    }

    /**
     * 根据父部门ID查询部门信息
     *
     * @param parentId
     * @return
     */
    public List<TabCoreOrgEntity> selectOrgAndPeoByPId(Integer parentId) {
        return tabCoreOrgDao.selectOrgAndPeoByPId(parentId);
    }

    public Integer selectMaxInOrder(Integer parentId){
        return tabCoreOrgDao.selectMaxInOrder(parentId);
    }

    
    /**
     * 新增部门
     * @param tabCoreOrg
     * @return
     */
    public int save(TabCoreOrgEntity tabCoreOrg,Integer parentId) {
        int num = tabCoreOrgDao.save(tabCoreOrg);
        if(parentId.intValue() == 0){ //新机构
            //增加功能默认角色
            TabFunRoleEntity role = new TabFunRoleEntity();
            role.setFunRoleName("默认角色");
            role.setFunRoleDesc("默认角色");
            role.setOrgId(tabCoreOrg.getOrgId());
            role.setFunRoleType(CommonEnum.FunRoleType.DEFAULT.getCode());
            role.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
            role.setCreateId(UserUtil.getCurrentUser().getUserId());
            role.setCreateTime(new Date());
            tabFunRoleDao.add(role);
            //给角色分配默认的功能
            List<TabCoreFunEntity> funList = tabCoreFunDao.findDefaultFunction();
            List<TabFunRoleRelationEntity> relList = new ArrayList<>();
            for(TabCoreFunEntity fun: funList){
                TabFunRoleRelationEntity relation = new TabFunRoleRelationEntity();
                relation.setFunId(fun.getFunId());
                relation.setFunRoleId(role.getFunRoleId());
                relation.setOrgId(tabCoreOrg.getOrgId());
                relation.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
                relation.setCreateId(UserUtil.getCurrentUser().getUserId());
                relation.setCreateTime(new Date());
                relList.add(relation);
            }
            tabFunRoleRelationDao.batchAdd(relList);
            //给机构分给默认权限
            Integer[] funIds =  new Integer[funList.size()];
            int  i =0;
            for(TabCoreFunEntity fun: funList){
                funIds[i] = fun.getFunId();
                i++;
            }
            tabOrgFunDao.bacthsave(tabCoreOrg.getOrgId(),funIds,UserUtil.getCurrentUser().getUserId());
        }

        return num;
    }

    /**
     * 修改部门
     * @param tabCoreOrg
     * @return
     */
    public int update(TabCoreOrgEntity tabCoreOrg){
        return tabCoreOrgDao.update(tabCoreOrg);
    }

    /**
     * 通过parentId分页查询
     * @param orgPageRequest
     * @return
     */
    public PagingResult<TabCoreOrgEntity> selectPageByParentId(OrgPageRequest orgPageRequest){
        return tabCoreOrgDao.selectPageByParentId(orgPageRequest);
    }

    /**
     * 通过parentIds查询子部门信息
     * @param parentIds
     * @return
     */
    public List<TabCoreOrgEntity> selectByParentIds(Integer[] parentIds) {
        return tabCoreOrgDao.selectByParentIds(parentIds);
    }

    /**
     * 逻辑批量删除部门信息
     * @param orgIds
     * @return
     */
    public int deleteLogicByIds(Integer[] orgIds){
        return tabCoreOrgDao.deleteLogicByIds(orgIds);
    }

    public Integer getTopParentOrgId(Integer orgId){
        return tabCoreOrgDao.getTopParentOrgId(orgId);
    }

    public List<TabCoreOrgEntity> findOrgAllMember(Integer userId){
        TabCoreUserEntity user = tabCoreUserDao.getUserInfoById(userId);
        Integer topOrg =  tabCoreOrgDao.getTopParentOrgId(user.getOrgId());
        List<TabCoreOrgEntity> list = this.getChildOrgById(topOrg);
        return list;
    }

    public List<TabCoreOrgEntity> getChildOrgById(int id){  //包括当前机构
        HashMap<Integer,List<TabCoreOrgEntity>> map = new HashMap<>();
        List<TabCoreOrgEntity> childList = new ArrayList<>();

        List<TabCoreOrgEntity> allOrgList = tabCoreOrgDao.selectAll();
        for (TabCoreOrgEntity org:allOrgList) {
            //加入当前机构
            if(org.getOrgId().intValue() == id){
                childList.add(org);
            }
            List<TabCoreOrgEntity> list = map.get(org.getParentId());
            if (list == null) {
                list = new ArrayList<TabCoreOrgEntity>();
            }
            list.add(org);
            map.put(org.getParentId(), list);
        }

        List<TabCoreOrgEntity> resList = new ArrayList<>();
        resList = getchilds(id,map,resList);
        childList.addAll(resList);

        return childList;
    }

    public  List<TabCoreOrgEntity> getchilds(int id,HashMap<Integer,List<TabCoreOrgEntity>> map,List<TabCoreOrgEntity> resList){
        List<TabCoreOrgEntity> list = map.get(id);
        if(list == null) return resList;
        resList.addAll(list);
        for (TabCoreOrgEntity entry: list) {
            getchilds(entry.getOrgId(),map,resList);
        }
        return resList;
    }
}
